<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">取消选课</a>
        </script>

    </div>
</div>
<script src="../../static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8888/student/allSelectCourse',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field: 'couId', width: 160, title: '课程编号', sort: true},
                {field: 'couName', width: 160, title: '课程名字'},
                {field: 'teaName', width: 160, title: '任课老师', sort: true},
                {field: 'credit', width: 160, title: '学分'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [3, 6, 9],
            limit: 3,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
		
		function deleteCourde(data){
			var str = "couId="+data.couId
			$.ajax({
			    type : "get",
			    async:true,
			    url : 'http://localhost:8888/student/deleteSelectCourse',
			    data : str,
			    xhrFields: { //
			        withCredentials: true,
			    },
			    success : function(resp){
			        if(resp.code === 0){
						layer.msg(resp.msg);
					}else{
						layer.msg(resp.msg);
					}
			    }
			});
		}

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
			var text = "真的取消您已选择的"+data.couName+"课吗？"
			if (obj.event === 'delete') {
                layer.confirm(text, function (index) {
					// alert(data.couId)
					// 删除选课的操作
					deleteCourde(data)
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>

</body>
</html>
